package com.zhiche.lisa.tms.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.tms.dao.model.ShipOrderItem;
import com.zhiche.lisa.tms.dao.model.Shipment;
import com.zhiche.lisa.tms.dto.ReceiptDTO;
import com.zhiche.lisa.tms.service.IShipOrderItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 运输任务
 * </p>
 */
@RestController
@RequestMapping("/shipOrderItem")
@Api(value = "/shipOrderItem", description = "运单货物明细接口",tags={"运单货物明细接口"})
public class ShipOrderItemController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ShipmentController.class);
    @Autowired
    private IShipOrderItemService shipOrderItemService;

    @GetMapping(value = "/getShipOrderItemById/{id}")
    @ApiOperation(value = "根据ID得到运单货物明细列表", notes = "根据ID得到运单货物明细列表", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    @ApiResponses({@ApiResponse(code = 0,message = "success",response = String.class),@ApiResponse(code = -1,message = "操作失败",response = Object.class)})
    public RestfulResponse<ShipOrderItem> getShipOrderItemById(@PathVariable Long id) {
        LOGGER.info("ShipOrderItemController.getShipOrderItemById（根据ID得到运单货物明细列表） params : {} ", id);
        RestfulResponse result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            ShipOrderItem orderItemData = shipOrderItemService.getShipOrderItemById(id);
            result.setData(orderItemData);
        } catch (BaseException e) {
            LOGGER.error("ShipOrderItemController.getShipOrderItemById ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("ShipOrderItemController.getShipOrderItemById ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @GetMapping(value = "/queryShipOrderItem")
    @ApiOperation(value = "/queryShipOrderItemApi",notes = "根据来源唯一键查询运单货物明细",response = RestfulResponse.class)
    public RestfulResponse<ShipOrderItem> queryShipOrderItem(String itemSourceKey ){
        LOGGER.info("controller:/ShipOrderItem/queryShipOrderItem data: {}", itemSourceKey);
        RestfulResponse result = new RestfulResponse(0,"成功",null);
        try {
            ShipOrderItem shipOrderItem = shipOrderItemService.queryShipOrderItem(itemSourceKey);
            result.setData(shipOrderItem);
        }catch (Exception e){
            result.setCode(-1);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    @GetMapping(value = "/queryPurchasePriceOtm/{id}")
    @ApiOperation(value = "从OTM获取暂估价", notes = "从OTM获取暂估价", produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<Map<String, Object>> queryPurchasePriceOtm(@PathVariable Long id){
        LOGGER.info("ShipOrderItemController.queryPurchasePriceOtm（根据ID得到支付暂估价） params : {} ", id);
        RestfulResponse<Map<String, Object>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(shipOrderItemService.queryPurchasePriceOtm(id));
        }catch (BaseException e){
            LOGGER.error("ShipOrderItemController.queryPurchasePriceOtm ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        }catch (Exception e){
            LOGGER.error("ShipOrderItemController.queryPurchasePriceOtm ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @GetMapping(value = "/getShipOrderItemByShipCode")
    @ApiOperation(value = "根据装车CODE得到运单货物明细列表", notes = "根据装车CODE得到运单货物明细列表", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    @ApiResponses({@ApiResponse(code = 0,message = "success",response = String.class),@ApiResponse(code = -1,message = "操作失败",response = Object.class)})
    public RestfulResponse<List<Map<String,Object>>> getShipOrderItemById(String code) {
        LOGGER.info("ShipOrderItemController.getShipOrderItemByShipCode（根据装车CODE得到运单货物明细列表） params : {} ", code);
        RestfulResponse result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            List<Map<String,Object>> lists = shipOrderItemService.getShipOrderItemByShipCode(code);
            result.setData(lists);
        } catch (BaseException e) {
            LOGGER.error("ShipOrderItemController.getShipOrderItemByShipCode ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("ShipOrderItemController.getShipOrderItemByShipCode ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }


    @PostMapping(value = "/cancelShip")
    @ApiOperation(value = "取消发运",notes = "取消发运",response = RestfulResponse.class)
    public RestfulResponse cancelShipTime(@RequestBody Map<String,Object> map){
        LOGGER.info("controller:/ShipOrderItem/cancelShipTime data: {}", map);
        RestfulResponse result = new RestfulResponse(0,"成功",null);
        try {
            result.setData(shipOrderItemService.cancelShipTime(map));
        }catch (BaseException ex) {
            LOGGER.error("controller:/ShipOrderItem/cancelShip ERROR...{} ", ex.getMessage());
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), ex.getMessage(), null);
        } catch (Exception e){
            result.setCode(-1);
            result.setMessage(e.getMessage());
            LOGGER.error("controller:/ShipOrderItem/cancelShip ERROR...{} ", e.getMessage());
        }
        return result;
    }

    @PostMapping(value = "/getReceiptList")
    @ApiOperation(value = "获取末端指令订单列表",notes = "获取末端指令订单列表",response = RestfulResponse.class)
    public RestfulResponse<Page<ReceiptDTO>> getReceiptList(@RequestBody Page<ReceiptDTO> page, @RequestHeader("Authorization") String token){
        LOGGER.info("controller:/ShipOrderItem/getReceiptList data: {}", page);
        long startTime = System.currentTimeMillis();
        RestfulResponse<Page<ReceiptDTO>>  result = new RestfulResponse(0,"成功",null);
        try {
            result.setData(shipOrderItemService.getReceiptList(page,token));
            long endTime = System.currentTimeMillis();
            LOGGER.info("getReceiptList time : {}", endTime - startTime);
        }catch (BaseException ex) {
            LOGGER.error("controller:/ShipOrderItem/getReceiptList ERROR... ", ex.getMessage());
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), ex.getMessage(), null);
        } catch (Exception e){
            result.setCode(-1);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @PostMapping(value = "/exportReceiptList")
    @ApiOperation(value = "导出回单订单列表",notes = "导出回单订单列表",response = RestfulResponse.class)
    public RestfulResponse<List<ReceiptDTO>> exportReceiptList(@RequestBody Map<String,Object> map, @RequestHeader("Authorization") String token){
        LOGGER.info("controller:/ShipOrderItem/exportReceiptList data: {}", map);
        long startTime = System.currentTimeMillis();
        RestfulResponse<List<ReceiptDTO>>  result = new RestfulResponse(0,"成功",null);
        try {
            result.setData(shipOrderItemService.exportReceiptList(map,token));
            long endTime = System.currentTimeMillis();
            LOGGER.info("exportReceiptList time : {}", endTime - startTime);
        }catch (BaseException ex) {
            LOGGER.error("controller:/ShipOrderItem/exportReceiptList ERROR... ", ex.getMessage());
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), ex.getMessage(), null);
        } catch (Exception e){
            result.setCode(-1);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @PostMapping(value = "/receiptStatus")
    @ApiOperation(value = "回单状态",notes = "回单状态",response = RestfulResponse.class)
    public RestfulResponse receiptStatus(@RequestBody Map<String,String> map){
        LOGGER.info("controller:/ShipOrderItem/receiptStatus data: {}", map);
        RestfulResponse result = new RestfulResponse(0,"成功",null);
        try {
            shipOrderItemService.receiptStatus(map);
        }catch (BaseException ex) {
            LOGGER.error("controller:/ShipOrderItem/receiptStatus ERROR... ", ex.getMessage());
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), ex.getMessage(), null);
        } catch (Exception e){
            result.setCode(-1);
            result.setMessage(e.getMessage());
        }
        return result;
    }


}
